package com.crab.c02link;


/**
 * @author zfd
 * @version v1.0
 * @date 2022/4/28 17:49
 */
public class MyLinkSet {

    public static void main(String[] args) {
        MyLinkSet myLinkSet = new MyLinkSet();
        myLinkSet.insert(0, 0);
        myLinkSet.insert(1, 1);
        myLinkSet.insert(2, 2);
        myLinkSet.insert(3, 3);
        System.out.println(myLinkSet.get(3).data);
        myLinkSet.output();
        System.out.println(myLinkSet.delete(2).getData());
        myLinkSet.output();

    }


    // 头节点
    private Node head;
    // 尾节点
    private Node tail;
    // 链表的实际长度
    private int size;


    /**
     * 插入元素
     */
    public void insert(int index, int data) {
        if (index < 0 || index > size) {
            throw new IndexOutOfBoundsException("索引越界");
        }

        Node insertNode = new Node(data);

        if (size == 0) {
            // 空链表
            head = insertNode;
            tail = insertNode;
        } else if (index == 0) {
            // 插入头部
            insertNode.next = head;
            head = insertNode;
        } else if (index == size) {
            // 插入尾部
            tail.next = insertNode;
            tail = insertNode;
        } else {
            // 插入中间
            Node preNode = get(index - 1);
            insertNode.next = preNode.next;
            preNode.next = insertNode;
        }
        size++;
    }

    /**
     * 查找
     *
     * @param index
     * @return
     */
    public Node get(int index) {
        if (index < 0 || index > size) {
            throw new IndexOutOfBoundsException("索引越界");
        }
        Node temp = head;
        // 顺序逐个访问
        for (int i = 0; i < index; i++) {
            temp = temp.next;
        }
        return temp;
    }


    /**
     * 链表节点 含数据和下个节点的引用
     */
    private static class Node {
        int data;
        Node next;

        public int getData() {
            return data;
        }

        public Node getNext() {
            return next;
        }

        Node(int data) {
            this.data = data;
        }
    }

    /**
     * 删除元素
     */
    public Node delete(int index) {
        if (index < 0 || index > size) {
            throw new IndexOutOfBoundsException("索引越界");
        }

        Node removeNode = null;

        if (index == 0) {
            // 头部
            removeNode.next = head;
            head = head.next;
        } else if (index == size) {
            // 尾部
            Node preNode = get(index - 1);
            removeNode = preNode.next;
            tail = preNode;
        } else {
            // 中间
            Node preNode = get(index - 1);
            Node nextNode = preNode.next.next;
            removeNode = preNode.next;
            preNode.next = nextNode;
        }
        size--;
        return removeNode;
    }

    public void output() {
        Node temp = head;
        while (temp != null) {
            System.out.print(temp.data);
            System.out.print(' ');
            temp = temp.next;
        }
        System.out.println();
    }

}
